<template>
  <div class="box">
    <el-form :model="queryParams" ref="queryForm" size="small" class="input" :inline="true" v-show="showSearch">
      <el-form-item>
        <el-input v-model="queryParams.vipNo" prefix-icon="el-icon-search" style="width:340px"
          placeholder="请输入宠主姓名/电话/宠物名称/宠物品种/备注" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.sex" placeholder="请选择宠物性别">
          <el-option v-for="item in sexList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.state" placeholder="请选择绝育情况">
          <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
        <el-button type="primary" class="btn" size="mini" @click="handleAdd"
          v-hasPermi="['pet:pets:add']">新增宠物</el-button>
        <el-button type="warning" class="btn" size="mini" @click="handleExport"
          v-hasPermi="['pet:pets:export']">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="petsList">
      <el-table-column label="宠物头像" align="center" prop="url">
        <template slot-scope="scope">
          <img :src="scope.row.url" style="width: 50%;" alt="">
        </template>
      </el-table-column>
      <el-table-column label="宠物名称" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex == 1 ? '弟弟' : '妹妹' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品种" align="center" prop="varNo" />
      <el-table-column label="生日" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="毛色" align="center" prop="color" />
      <el-table-column label="绝育情况" align="center" prop="isSter">
        <template slot-scope="scope">
          <span>{{ scope.row.sex == isSter ? '已绝育' : '未绝育' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pet:pets:edit']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pet:pets:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pet:pets:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改宠物管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" class="center" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="input" label-width="100px">
        <el-form-item label="宠主" prop="vipNo">
          <el-select v-model="form.vipNo" placeholder="请选择" style="width: 100%;">
            <el-option :label="item.name" :value="item.no" v-for="item in vipList" :key="item.no"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宠物名称" prop="name">
          <el-input v-model="form.name" clearable placeholder="请输入宠物名称" />
        </el-form-item>
        <el-form-item label="品种" prop="varNo">
          <el-select v-model="form.varNo" placeholder="请选择" style="width: 100%;">
            <el-option :label="item.name" :value="item.id" v-for="item in varList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="phone">
          <el-radio v-model="form.sex" label="1">弟弟</el-radio>
          <el-radio v-model="form.sex" label="2">妹妹</el-radio>
        </el-form-item>
        <el-form-item label="宠物生日" prop="date">
          <el-date-picker clearable v-model="form.date" style="width:100%" type="date" value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="毛色" prop="color">
          <el-input v-model="form.color" placeholder="请输入毛色" />
        </el-form-item>
        <el-form-item label="肩高/体长" prop="high">
          <el-input v-model="form.high" class="input-radius-right" placeholder="请输入肩高/体长">
            <template slot="append">cm</template>
          </el-input>
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" class="input-radius-right" placeholder="请输入体重">
            <template slot="append">kg</template>
          </el-input>
        </el-form-item>
        <el-form-item label="绝育情况" prop="isSter">
          <el-radio v-model="form.isSter" label="1">已绝育</el-radio>
          <el-radio v-model="form.isSter" label="2">未绝育</el-radio>
        </el-form-item>
        <el-form-item label="宠物头像" prop="url">
          <el-upload class="avatar-uploader" :action="action" :headers="headers" :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="form.url" :src="form.url" class="avatar">
            <i v-else class="el-icon-upload avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="5" maxlength="200" show-word-limit placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="btn" style="padding:12px 70px" @click="submitForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPets, getPets, delPets, addPets, updatePets, getVipMap } from "@/api/pet/pets";
import { getVarList } from "@/api/pet/var"
import { getToken } from "@/utils/auth";
export default {
  name: "Pets",
  data() {
    return {
      action: process.env.VUE_APP_BASE_API + '/pet/upload/comFile',
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      sexList: [],//性别下拉
      stateList: [],//绝育情况
      vipList: [],
      varList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宠物管理表格数据
      petsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vipNo: null,
        name: null,
        varNo: null,
        sex: null,
        date: null,
        color: null,
        high: null,
        weight: null,
        isSter: null,
        url: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询宠物管理列表 */
    getList() {
      this.loading = true;
      listPets(this.queryParams).then(response => {
        this.petsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //宠主下拉
    getVipMap() {
      getVipMap().then(res => {
        if (res.code == 200) {
          this.vipList = res.data
        }
      })
    },
    //获取品种下拉
    getVarList() {
      getVarList().then(res => {
        if (res.code == 200) {
          this.varList = res.data
        }
      })
    },
    //上传图像
    handleAvatarSuccess(res, file) {
      this.form.url = res.url
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vipNo: null,
        name: null,
        varNo: null,
        sex: null,
        date: null,
        color: null,
        high: null,
        weight: null,
        isSter: null,
        url: null,
        remark: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getVipMap()
      this.getVarList()
      this.open = true;
      this.title = "新增宠物";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPets(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑宠物";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePets(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPets(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除宠物管理编号为"' + ids + '"的数据项？').then(function () {
        return delPets(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/pets/export', {
        ...this.queryParams
      }, `pets_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
